/**
 * Created by WYH on 17/4/14.
 */
angular.module('tribalc').controller(
    'StaffListController',
    ['$scope', '$rootScope', 'Enum', 'ElePosition', 'EleStyle', 'Api', 'StaffModel','Alert',
        function ($scope, $rootScope, Enum, ElePosition, EleStyle, Api, StaffModel,Alert) {

            getFocus();
            $scope.EleStyle = EleStyle;

            $scope.optionItems = Enum.StaffOptions;
            $scope.submitFilter = submitFilter;
            $scope.clickAdd = clickAdd;
            $scope.tableHeadItems = ['姓名', '性别', '联系电话', '身份证号', '部门', '职位', '门卡号码','状态', ''];

            $scope.showAddWindow = false;
            $scope.showModifyWindow = false;

            $scope.showStatusButton = false;
            $scope.status = true;

            $scope.additionalStaff = new StaffModel();
            $scope.modifiedStaff = new StaffModel();
            $scope.clickModifyStaff = clickModifyStaff;
            $scope.submitModifyStaff = submitModifyStaff;
            $scope.submitAddStaff = submitAddStaff;
            $scope.getFocusStaffInformation = getFocusStaffInformation;
            $scope.leaveFocusStaffInformation = leaveFocusStaffInformation;
            $scope.getClickInformation = getClickInformation;
            $scope.changeSelectedClass = changeSelectedClass;
            $scope.clickDelete = clickDelete;
            $scope.staffDelect=staffDelect;
            $scope.nextPageClick = nextPageClick;
            $scope.setManageClick = setManageClick;
            $scope.clickModifyStaffStatus = clickModifyStaffStatus;
            $scope.nextSkip = null;
            $scope.hasMore = false;
            $scope.pageNum = 1;

            getStaffList(null, null,function(){
                isShowUpdate()
            });

            function setManageClick(staffId) {
                Api.setManagestaff(staffId,function (result) {
                    console.log(result)
                    Alert.alertBox('修改成功',null);
                    $scope.showModifyWindow = false;
                    getStaffList();
                });
            }
            function nextPageClick(){
                if ($scope.hasMore == true)
                {
                    getStaffList(null,$scope.nextSkip,function () {
                        $scope.pageNum ++;
                    });
                }
                else {
                    Alert.alertBox("已经是最后一页",null);
                }
            }
            function clickDelete() {
                if (!$scope.selectedStaff || $scope.selectedStaff=='balck') {
                    Alert.alertBox('尚未选中员工，无法删除');
                } else {
                    $scope.showStaffDelectbox = true;

                }
            }

            function staffDelect() {
                var staffIds = [$scope.selectedStaff];
                console.log(staffIds)
                Api.deleteStaff(staffIds, function (result) {
                    window.location.reload()
                });
            }

            function clickModifyStaff(staff) {
                $scope.modifiedStaff = new StaffModel(staff);
                $scope.showModifyWindow = true;
                if ($scope.modifiedStaff.staffBindStatus == "BIND_ED" && ($scope.modifiedStaff.roles == null||($scope.modifiedStaff.roles != null && $scope.modifiedStaff.roles.length == 0)))
                {
                    $scope.isAbleClick = true;
                }
                else
                {
                    $scope.isAbleClick = false;
                }

            }


            function getFocusStaffInformation(staff) {
                $scope.hoverStaff = staff;

            }
            function leaveFocusStaffInformation(staff) {
                $scope.hoverStaff = !staff;
            }

            function getClickInformation(staff) {
                console.log(staff)
                $scope.selectedStaff = staff;
            }

            function getFocus() {
                $("*").click(function (event) {
                    if (!$(this).hasClass('sta-tr-row-tbody') && !$(this).hasClass('tool-title-delect')){
                        $scope.selectedStaff='balck';
                        $scope.$apply(function () {
                            changeSelectedClass(null)
                        })
                    }
                })
            }



            function getFocusStaffInformation(staff) {
                $scope.hoverStaff = staff;

            }
            function leaveFocusStaffInformation(staff) {
                $scope.hoverStaff = !staff;
            }

            function getClickInformation(staff) {
                console.log(staff)
                $scope.selectedStaff = staff;
            }
            function getFocus() {
                $("*").click(function (event) {
                    if (!$(this).hasClass('sta-tr-row-tbody') && !$(this).hasClass('tool-title-delect')){
                        $scope.selectedStaff='balck';
                        $scope.$apply(function () {
                            changeSelectedClass(null)
                        })
                    }
                    event.stopPropagation();
                })
            }

            function changeSelectedClass(staff) {
                if (staff == $scope.selectedStaff) {
                    return 'staff-triangl-select-img';
                }
                else if (staff == $scope.hoverStaff) {
                    return 'staff-triangl-hover-img';
                } else if($scope.selectedStaff=='balck'){
                    return '';
                }
                else {
                    return '';
                }
            }

            function submitModifyStaff(staff) {
                if (staff.isValid()) {
                    Api.modifyStaff(staff.id, staff, function (result) {
                        Alert.alertBox('修改成功',null);
                        $scope.showModifyWindow = false;
                        getStaffList();
                    });
                }
            }


            function submitAddStaff(staff) {
                if (staff.isValid()) {
                    Api.addStaff(staff, function (result) {
                        Alert.alertBox('添加成功',null);
                        $scope.showAddWindow = false;
                        getStaffList();
                        $scope.additionalStaff = new StaffModel();
                    })
                }
            }

            /* 提交筛选信息 */
            function submitFilter(key, value) {
                getStaffList(key + ':' + value, null, function (result) {

                    Alert.alertBox('筛选成功',null);
                });
            }

            function clickAdd() {
                $scope.showAddWindow = true;
            }

            function isShowUpdate(){
                $scope.showUpload=$scope.staffList.length !==0;
            }


            /* 获取员工列表列表 */
            function getStaffList(condition, sortSkip, sucFuc) {
                Api.getStaffList(condition, null, sortSkip, function (result) {
                    $scope.staffListStatus = result.data.content;
                    $scope.nextSkip = result.data.nextSkip;
                    $scope.hasMore = result.data.hasMore;
                    $scope.staffList = StaffModel.setupStaffList(result.data.content);
                    sucFuc ? sucFuc(result) : '';
                });
            }

            function clickModifyStaffStatus(staff) {
                var value =staff.phone;
                console.log(value);
                var companyId = $rootScope.RootCompanyInfo.id;
                Api.bindCompanyStaff(value,companyId,function(result){
                    window.location.reload()
                })
            }


        }]
);